﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Forums.OfflineStore;
using System.Configuration;
using Microsoft.Forums.ForumsClient.ThreadedView;
using System.Windows.Threading;

namespace Microsoft.Forums.ForumsClient
{
    /// <summary>
    ///  Observable collection of subscribed forums. This would be bound to the Subscribed Forums Folder
    ///  on the GUI. Add support to remove items when implementing unsubscribe
    /// </summary>
    public class SubscribedForumsCollection : ObservableCollection<ForumContainer>
    {
        /// <summary>
        ///  Constructor. Access any subscribed forums from the database and add to this collection.
        /// </summary>
        public SubscribedForumsCollection()
        {
           
        }

        /// <summary>
        /// 
        /// Updates the collection item with data from the Database for specific forumId.
        /// This is typically invoked after synchronization to update the data on the GUI from the database
        /// 
        /// </summary>
        /// <param name="forumId">Guid of the forum that is being refreshed</param>
        public void Refresh(Guid forumId)
        {
            if (forumId == null)
                throw new ArgumentNullException("forumId");
            
            // Only single ForumContainer should exist in this list that matches this forumId
            var forumContainer = this.Single(fc => fc.Forum.Id == forumId);

            forumContainer.PopulateRootPosts();
            forumContainer.RefreshPostCounts();
        }

        /// <summary>
        ///  Indexer that returns the ForumContainer object that matches the given forumId
        /// </summary>
        /// <param name="forumId">Forum Id Guid</param>
        /// <returns>ForumContainer if the Guid exists or null</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1043:UseIntegralOrStringArgumentForIndexers")]
        public ForumContainer this[Guid forumId]
        {
            get
            {
                if (forumId == null)
                    throw new ArgumentNullException("forumId");

                ForumContainer forumContainer;

                // Only single ForumContainer should exist in this list that matches this forumId
                try
                {
                    forumContainer = this.Single(fc => fc.Forum.Id == forumId);
                }
                catch(InvalidOperationException)
                {
                    // Just so that the caller doesn't have to handle this exception
                    // InvalidOperationException is thrown when there is no forum in the collection
                    // with the given valid guid.
                    forumContainer = null;
                }

                return forumContainer;
            }
        }
    }
   
}
